
/**
 * 给 bootstrap tab 控件注册点击事件，当发生切换时，将切换到的 tab 的地址（href）保存到 localstorage 中
 * @param {string} currentKey 保存在 localstorage 中的位置的键值，应具备唯一性，排他性
 */
function registerLastTabHandler(currentKey) {
    // for bootstrap 3 use 'shown.bs.tab', for bootstrap 2 use 'shown' in the next line
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        var lastKey = localStorage.getItem('lastItemKey')
        if (lastKey && lastKey != currentKey) {
            console.log('delete last item: ' + lastKey)
            localStorage.removeItem(lastKey)
        }

        // save the latest tab; use cookies if you like 'em better:      
        // console.log(currentKey)
        var item = $(this).attr('href')
        localStorage.setItem(currentKey, item);
        localStorage.setItem('lastItemKey', currentKey)
        console.log('save last key ' + currentKey + "to: " + item)
    });
}

/**
 * 进入包含有 tab 控件的界面时调用，自动切换到之前的 tab 中
 * @param {string} currentKey 
 */
function switchToTab(currentKey) {
    // go to the latest tab, if it exists:
    var item = localStorage.getItem(currentKey);
    console.log("get key " + currentKey + ": " + item)
    if (item) {
        $('[href="' + item + '"]').tab('show');
    }
}